From: Keir Fraser Date: Tue, 18 May 2010 10:38:12 +0000 (+0100) Subject: xl: allow scaling suffix on memory sizes in mem-set and mem-max X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~12133 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https:/%22bookmarks://%22Dat/%22http:/www.example.com/cgi/%22https:/%22bookmarks:/%22Dat?a=commitdiff_plain;h=6cbd479e30dc414af9b892e96412920a753391cd;p=xen.git xl: allow scaling suffix on memory sizes in mem-set and mem-max Allow mem-set and mem-max to take 'b', 'k', 'm', 'g' and 't' as scaling suffixes for bytes, kilobytes, mega, etc. An unadorned number is still treated as kilobytes so no existing users should be affected. Signed-off-by: Jeremy Fitzhardinge Signed-off-by: Keir Fraser --- diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index afadeda1d1..b5d2eba022 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -1200,16 +1200,45 @@ void help(char *command) } } -int set_memory_max(char *p, char *mem) +static int64_t parse_mem_size_kb(char *mem) { char *endptr; - uint32_t memorykb; + int64_t kbytes; + + kbytes = strtoll(mem, &endptr, 10); + + if (strlen(endptr) > 1) + return -1; + + switch (tolower(*endptr)) { + case 't': + kbytes <<= 10; + case 'g': + kbytes <<= 10; + case 'm': + kbytes <<= 10; + case '\0': + case 'k': + break; + case 'b': + kbytes >>= 10; + break; + default: + return -1; + } + + return kbytes; +} + +int set_memory_max(char *p, char *mem) +{ + int64_t memorykb; int rc; find_domain(p); - memorykb = strtoul(mem, &endptr, 10); - if (*endptr != '\0') { + memorykb = parse_mem_size_kb(mem); + if (memorykb == -1) { fprintf(stderr, "invalid memory size: %s\n", mem); exit(3); } @@ -1255,17 +1284,17 @@ int main_memmax(int argc, char **argv) void set_memory_target(char *p, char *mem) { - char *endptr; - uint32_t memorykb; + long long int memorykb; find_domain(p); - memorykb = strtoul(mem, &endptr, 10); - if (*endptr != '\0') { + memorykb = parse_mem_size_kb(mem); + if (memorykb == -1) { fprintf(stderr, "invalid memory size: %s\n", mem); exit(3); } - printf("setting domid %d memory to : %d\n", domid, memorykb); + + printf("setting domid %d memory to : %lld\n", domid, memorykb); libxl_set_memory_target(&ctx, domid, memorykb, /* enforce */ 1); } diff --git a/tools/libxl/xl_cmdtable.c b/tools/libxl/xl_cmdtable.c index 68b8279b92..82cf2ca722 100644 --- a/tools/libxl/xl_cmdtable.c +++ b/tools/libxl/xl_cmdtable.c @@ -110,12 +110,16 @@ struct cmd_spec cmd_table[] = { }, { "mem-max", &main_memmax, - "Set the maximum amount reservation for a domain", + "Set the maximum amount reservation for a domain.\n" + "Units default to kilobytes, but can be suffixed with\n" + "'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)", " ", }, { "mem-set", &main_memset, - "Set the current memory usage for a domain", + "Set the current memory usage for a domain.\n" + "Units default to kilobytes, but can be suffixed with\n" + "'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)", " ", }, { "button-press",